package com.gzx.plugin.tjzy.modular.sms.receipt.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzx.plugin.tjzy.modular.sms.receipt.bo.GzxStatisticSmsReceiptBo;
import com.gzx.plugin.tjzy.modular.sms.receipt.mapper.GzxSmsReceiptMapper;
import com.gzx.plugin.tjzy.modular.sms.receipt.service.GzxSmsReceiptStatisticService;
import com.gzx.plugin.tjzy.modular.sms.receipt.vo.GzxSmsReceiptStatisticVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xbl
 */
@Service
@DS("sms")
@RequiredArgsConstructor
public class GzxSmsReceiptStatisticServiceImpl implements GzxSmsReceiptStatisticService {

    private final GzxSmsReceiptMapper smsReceiptMapper;

    /**
     * 短信送达率
     */
    @DS("sms")
    @Override
    public Page<GzxSmsReceiptStatisticVo> smsSendRate(GzxStatisticSmsReceiptBo bo) {
        Page<GzxSmsReceiptStatisticVo> page = new Page<>(bo.getCurrent(), bo.getSize());

        int current = bo.getCurrent();
        int pageNum = (current - 1) * bo.getSize();
        bo.setCurrent(pageNum);

        List<GzxSmsReceiptStatisticVo> list = smsReceiptMapper.smsSendRate(bo);

        //计算送达率
        for (GzxSmsReceiptStatisticVo vo : list) {
            vo.setSmsSendRate(Double.parseDouble(String.format("%.2f", (double) vo.getSuccessSend() / vo.getTotalSend())));
        }

        page.setRecords(list);

        long total = smsReceiptMapper.smsSendRateTotal(bo);
        page.setTotal(total);

        return page;
    }
}
